package site.lanmushan.framework.datascope.interceptor.autoupdate;

import com.alibaba.fastjson.JSONObject;
import groovy.transform.AutoFinal;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.update.Update;
import org.springframework.beans.factory.annotation.Autowired;
import site.lanmushan.framework.datascope.annotation.DataControl;
import site.lanmushan.framework.datascope.bean.DataControlUserContext;
import site.lanmushan.framework.datascope.constant.DataControlType;
import site.lanmushan.framework.datascope.properties.AutoUpdateField;
import site.lanmushan.framework.datascope.properties.AutoUpdateProperties;
import site.lanmushan.framework.datascope.support.DataControlUserContextProvider;
import site.lanmushan.framework.util.utils.DateUtil;
import site.lanmushan.groovyscript.GroovyScriptUtil;

import java.sql.Connection;
import java.util.*;

/**
 * @author Administrator
 */
//@DataControl(DataControlType.UPDATE)
@Slf4j
public class UpdateAutoUpdateInterceptor implements AutoUpdateInterceptor {
    @Autowired
    AutoUpdateProperties autoUpdateProperties;
    @Autowired
    private DataControlUserContextProvider dataControlUserContextProvider;
    @Override
    public Statement doIntercept(Statement statement) throws JSQLParserException {
        DataControlUserContext userContext = dataControlUserContextProvider.getUserContext();
        Map<String, String> tableMap = new HashMap<>(4);
        Update update = (Update) statement;
        Table table = update.getTable();
        String tableName=table.getName();
        List<AutoUpdateField> ruleList = null;
        //忽略这个表
        if(autoUpdateProperties.getIgnore().contains(tableName))
        {
            return statement;
        }
        //有没有指定规则
        if(autoUpdateProperties.getDefinite().getUpdate().containsKey(tableName))
        {
            ruleList=autoUpdateProperties.getDefinite().getUpdate().get(tableName);
        }
        List<Column> columnList = update.getColumns();
        if(ruleList==null)
        {
            ruleList=autoUpdateProperties.getGlobal().getUpdate();
        }
        List<Column> newColumnList=new LinkedList<>();
        List<Expression> newExpressionList=new LinkedList<>();
        for (AutoUpdateField field:ruleList) {
            Boolean exist = columnList.stream().filter(it -> it.getColumnName().equals(field.getFieldName())).findAny().isPresent();
            if (!exist) {
                newColumnList.add(new Column(field.getFieldName()));
                String content= JSONObject.toJSONString(field.getFieldValue());
                JSONObject json = (JSONObject) JSONObject.toJSON(userContext);
                json.put("nowFormat", DateUtil.nowFormat());
                String   result = (String) GroovyScriptUtil.execute(content, userContext, json.getInnerMap(), String.class);
                Expression expression = CCJSqlParserUtil.parseExpression(result);
                newExpressionList.add(expression);
            }
        }




        update.getColumns().addAll(newColumnList);

        update.getExpressions().addAll(newExpressionList);

        return statement;
    }
}
